Page({
	data: {	
		isShowRules:false,

		//抽奖组1的位移
		drawOneY:-300,
		drawOneYs:-100,

		//抽奖组2的位移
		drawTwoY:-500,
		drawTwoYs:-200,

		//抽奖组3的位移
		drawThreeY:-400,
		drawThreeYs:-300,

		flag:false,  //是否在抽奖进行中

		prize:0,    //1 -- 手机  2 -- 拍立得  3 -- 护手霜

		isShowDrawNoNum:false,  //显示积分不足弹窗
		isShowDrawNoPrize:false,  //未中奖弹窗

		pauseDraw:false,

		mineEnergy:0,

		isDrawEndTag:false
	},

	//跳转页面
	goPage(e){
		my.navigateTo({
			url: e.target.dataset.url
		});
	},

	//开始抽奖
	startDraw(){

		if(this.data.flag)return

		getApp().cloud.function.invoke('draw',{},"draw").then(res => {
			console.log(res); 



			clearInterval(this.DrawEnd)

			if(res.code == 0){

				this.startOne()

				this.startTwo()

				this.startThree()

				this.setData({
					prize:res.data.level
				})

				this.DrawEnd = setInterval(() => {
					if(this.data.isDrawEndTag){
						
						setTimeout(() => {
								my.navigateTo({
									url: '../minePrize/minePrize'
								});

								this.setData({
									isDrawEndTag:false
								})

						}, 400);

						clearInterval(this.DrawEnd)
						
					}
				},10)
			
				

			}else if(res.code == -2){

				this.setData({
					prize:0,
					// pauseDraw:true,
					isShowDrawNoNum:true,
					isDrawEndTag:false,
					flag:false
				})

				// this.DrawEnd = setInterval(() => {
				// 	if(this.data.isDrawEndTag){

				// 		setTimeout(() => {
				// 			this.setData({
				// 				prize:0,
				// 				isShowDrawNoNum:true,
				// 				isDrawEndTag:false
				// 			})
				// 		}, 400);

				// 		clearInterval(this.DrawEnd)
						
				// 	}
				// },10)

			}else if(res.code == -3){
				this.setData({
					prize:0
				})

				this.startOne()

				this.startTwo()

				this.startThree()
				

				this.DrawEnd = setInterval(() => {
					
					if(this.data.isDrawEndTag){

						clearInterval(this.DrawEnd)

						setTimeout(() => {
							this.setData({
								prize:0,
								isDrawEndTag:false
							})

							my.navigateTo({
								url: '../drawResult/drawResult?end=noPrize'
							});
						}, 400);

						
					}

				},10)

				
			}else if(res.code == -4){
				// this.setData({
				// 	prize:0
				// })

				this.setData({
					prize:0,
					// pauseDraw:true,
					isDrawEndTag:false,
					flag:false
				})

				// setTimeout(() => {
					my.navigateTo({
						url: '../drawResult/drawResult?end=abc'
					});
				// }, 400);

				// this.DrawEnd = setInterval(() => {
				// 	if(this.data.isDrawEndTag){

				// 		setTimeout(() => {
				// 			this.setData({
				// 				prize:0,
				// 				isDrawEndTag:false
				// 			})

				// 			my.navigateTo({
				// 				url: '../drawResult/drawResult?end=abc'
				// 			});
				// 		}, 400);

				// 		clearInterval(this.DrawEnd)
						
				// 	}
				// },10)

				
			}

		})  

		this.setData({
			flag:true
		})

		clearInterval(this.setOne)
		clearInterval(this.setTwo)
		clearInterval(this.setThree)

		setTimeout(() => {
			// this.startOne()

			// this.startTwo()

			// this.startThree()
		}, 100);

		

	},

	// 开始转动第一个抽奖组
	startOne(){
		let y = this.data.drawOneY
		let y1 = this.data.drawOneYs
		let ac = 0

		let seeped = 6

		let count = 0;


		this.setOne = setInterval(() => {

			if(ac == 6 || this.data.pauseDraw){
				clearInterval(this.setOne)

				if(this.data.prize == 1){
					this.setData({
						drawOneY:-300,
						drawOneYs:-100
					})
				}else if(this.data.prize == 2){
					this.setData({
						drawOneY:-500,
						drawOneYs:-200
					})
				}else if(this.data.prize == 3){
					this.setData({
						drawOneY:-400,
						drawOneYs:-300
					})
				}else{
						this.setData({
							drawOneY:-300,
							drawOneYs:-100
						})
				}

				return;
			}

			if(count >= 12){

				count = 0;

				if(ac < 4){
					seeped = seeped + 2 > 20 ? 20 : seeped + 2
				}else{
					seeped = seeped - 2 < 10 ? 10 : seeped - 2
				}
			}

			if(ac == 5){
				this.setData({
					drawOneY:this.data.drawOneY >= 100 ? -500 : this.data.drawOneY + seeped,
					drawOneYs:this.data.drawOneYs >= 100 ? -500 : this.data.drawOneYs + seeped
				})
			}else{
				this.setData({
					drawOneY:this.data.drawOneY >= 100 ? -500 : this.data.drawOneY + seeped,
					drawOneYs:this.data.drawOneYs >= 100 ? -500 : this.data.drawOneYs + seeped
				})
			}

			

			if(this.data.drawOneY >= 100)ac += 1

			count += 1

			
		},20)
	},

	// 开始转动第二个抽奖组
	startTwo(){
		let y = this.data.drawTwoY
		let y1 = this.data.drawTwoYs
		let ac = 0

		let seeped = 1

		let count = 0;


		this.setTwo = setInterval(() => {

			if(ac == 6 || this.data.pauseDraw){
				clearInterval(this.setTwo)

				if(this.data.prize == 1){
					this.setData({
						drawTwoY:-300,
						drawTwoYs:-100
					})
				}else if(this.data.prize == 2){
					this.setData({
						drawTwoY:-500,
						drawTwoYs:-200
					})
				}else if(this.data.prize == 3){
					this.setData({
						drawTwoY:-400,
						drawTwoYs:-300
					})
				}else{
					this.setData({
						drawTwoY:-500,
						drawTwoYs:-200
					})
				}

				return;
			}

			if(count >= 12){

				count = 0;

				if(ac < 4){
					seeped = seeped + 2 > 20 ? 20 : seeped + 2
				}else{
					seeped = seeped - 2 < 10 ? 10 : seeped - 2
				}
			}

			if(ac == 5){
				this.setData({
					drawTwoY:this.data.drawTwoY >= 100 ? -500 : this.data.drawTwoY + seeped,
					drawTwoYs:this.data.drawTwoYs >= 100 ? -500 : this.data.drawTwoYs + seeped
				})
			}else{
				this.setData({
					drawTwoY:this.data.drawTwoY >= 100 ? -500 : this.data.drawTwoY + seeped,
					drawTwoYs:this.data.drawTwoYs >= 100 ? -500 : this.data.drawTwoYs + seeped
				})
			}

			// this.setData({
			// 	drawTwoY:this.data.drawTwoY >= 100 ? -500 : this.data.drawTwoY + seeped,
			// 	drawTwoYs:this.data.drawTwoYs >= 100 ? -500 : this.data.drawTwoYs + seeped
			// })

			if(this.data.drawTwoY >= 100)ac += 1

			count += 1

			
		},20)
	},

	// 开始转动第三个抽奖组
	startThree(){
		let y = this.data.drawThreeY
		let y1 = this.data.drawThreeYs
		let ac = 0

		let seeped = 0

		let count = 0;


		this.setThree = setInterval(() => {

			if(ac == 6 || this.data.pauseDraw){
				clearInterval(this.setThree)

				console.info(2)

				if(this.data.prize == 1){
					this.setData({
						drawThreeY:-300,
						drawThreeYs:-100,
						flag:false,
						pauseDraw:false,
						isDrawEndTag:true
					})
				}else if(this.data.prize == 2){
					this.setData({
						drawThreeY:-500,
						drawThreeYs:-200,
						flag:false,
						pauseDraw:false,
						isDrawEndTag:true
					})
				}else if(this.data.prize == 3){
					this.setData({
						drawThreeY:-400,
						drawThreeYs:-300,
						flag:false,
						pauseDraw:false,
						isDrawEndTag:true
					})
				}else{
					this.setData({
						drawThreeY:-400,
						drawThreeYs:-300,
						flag:false,
						pauseDraw:false,
						isDrawEndTag:true
					})
				}

				return;
			}

			if(count >= 12){

				count = 0;

				if(ac < 4){
					seeped = seeped + 2 > 20 ? 20 : seeped + 2
				}else{
					seeped = seeped - 2 < 10 ? 10 : seeped - 2
				}
			}

			if(ac == 5){
				this.setData({
					drawThreeY:this.data.drawThreeY >= 100 ? -500 : this.data.drawThreeY + seeped,
					drawThreeYs:this.data.drawThreeYs >= 100 ? -500 : this.data.drawThreeYs + seeped
				})
			}else{
				this.setData({
					drawThreeY:this.data.drawThreeY >= 100 ? -500 : this.data.drawThreeY + seeped,
					drawThreeYs:this.data.drawThreeYs >= 100 ? -500 : this.data.drawThreeYs + seeped
				})
			}
			

			// this.setData({
			// 	drawThreeY:this.data.drawThreeY >= 100 ? -500 : this.data.drawThreeY + seeped,
			// 	drawThreeYs:this.data.drawThreeYs >= 100 ? -500 : this.data.drawThreeYs + seeped
			// })

			if(this.data.drawThreeY >= 100)ac += 1

			count += 1

			
		},20)
	},

	showRules(){
		this.setData({
			isShowRules:true
		})
	},

	//获取自己的能量值
	mineTop(){
		getApp().cloud.function.invoke('client',{},"getCurrentUserInfo").then(res => {
			console.log(res); 

			if(res.code != 0){
				my.showToast({content:res.msg});
				return
			}

			this.setData({
				mineEnergy:res.data.current_energy
			})
			
			

		}) 
	},

	//继续游戏
	againGame(){
		//开始游戏前判断剩余游戏次数是否充足

		my.showLoading();

		getApp().cloud.function.invoke('client',{},"getCurrentUserInfo").then(res => {
			console.log('获取剩余游戏次数=====>',res); 

			my.hideLoading();

			if(res.data.game_left_num <= 0){
				my.navigateTo({
					url: '../drawResult/drawResult?end=gameNo'
				});
			}else{
				my.navigateTo({
					url: '../game/game'
				});
			}
		})
		
		
	
	},

	//回到首页
	backMain(){
		my.reLaunch({
			url: '../index/index'
		});
	},
	onShow(){
		this.mineTop()

		this.setData({
			pauseDraw:false,

			mineEnergy:0,

			isDrawEndTag:false
		})
	},
  onLoad() {},
});
